package com.jic.data.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.jic.data.constant.consist.ResponseCode;
import com.jic.data.exception.AppRuntimeException;
import com.jic.data.model.entity.MonOneMoreGuestBaseCategory;
import com.jic.data.model.vo.response.HistogramChartResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import javax.annotation.Resource;
import java.math.BigDecimal;
import com.jic.data.model.vo.response.SaleOverviewBaseDataResponse;
import com.jic.data.model.entity.SaleOverviewBaseData;
import com.jic.data.mapper.SaleOverviewBaseDataMapper;
import com.jic.data.service.SaleOverviewBaseDataService;

import lombok.extern.slf4j.Slf4j;
/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class SaleOverviewBaseDataServiceImpl implements SaleOverviewBaseDataService {

	@Resource
	private SaleOverviewBaseDataMapper mapper;


	/**
	 * 销售概览-销售概览基本数据
	 *
	 * @param request 不分页查询入参实体类
	 * @return 数据库查到的实体类列表
	 */
	@Override
	public SaleOverviewBaseDataResponse listAllRecord(SaleOverviewBaseData request) {
		SaleOverviewBaseData data = mapper.queryTotalAmountOrderPrd(request);
		SaleOverviewBaseDataResponse response = new SaleOverviewBaseDataResponse();
		BeanUtils.copyProperties(data, response);

		BigDecimal totalAmount = data.getSaleTotalAmount();
		Integer order = data.getTotalOrderQuantity();
		Integer product = data.getTotalProductQuantity();

		response.setDiscountRate(new BigDecimal(0.98));
		response.setOrderUnitNumber(product / order);
		response.setCustomersUnitPrice(totalAmount.subtract(new BigDecimal(order.intValue())));

		return response;
	}

	/**
	 * 销售概览-销售趋势
	 * @param
	 * @return
	 */
	@Override
	public HistogramChartResponse queryEveryDayAmountOrder(SaleOverviewBaseData request){
		List<SaleOverviewBaseData> list = mapper.queryEveryDayAmountOrder(request);

		HistogramChartResponse response = new HistogramChartResponse();
		List<String> xDate = new ArrayList<>();
		List<Double> yAmount = new ArrayList<>();
		List<Double> yOrder = new ArrayList<>();

		list.stream().forEach( category -> {
			xDate.add(DateUtil.format(category.getSaleDate(),"yyyy-MM-dd"));
			yAmount.add(category.getSaleTotalAmount().doubleValue() / 1000);
			yOrder.add(category.getTotalOrderQuantity().doubleValue() / 1000);
		});
		response.setX(xDate);
		response.setY1(yAmount);
		response.setY2(yOrder);

		return response;
	}


}
